package com.lzw.breezeNovel.webApp.listen;

import com.lzw.breezeNovel.model.entity.pojo.user.LoginLog;
import com.lzw.breezeNovel.model.enums.user.BaseStatus;
import com.lzw.breezeNovel.webApp.event.UserFindPassowrdEvent;
import com.lzw.breezeNovel.webApp.event.UserLoginEvent;
import com.lzw.breezeNovel.webApp.event.UserRegisterEven;
import com.lzw.breezeNovel.webApp.mapper.common.LoginLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.listen
 * @date 2025/2/28
 * @description 用户登入监听器
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class UserEventListener {

    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 监听注册信息
     * @param event
     */
    @EventListener
    public void onListenRegister(@NotNull UserRegisterEven event) {
        log.info("用户注册成功，手机号:{}",event.getPhone());
        //记录日志
        LoginLog loginLog = new LoginLog();
        loginLog.setStatus(BaseStatus.ENABLE);
        loginLog.setIpaddr(UUID.randomUUID().toString());
        loginLog.setMsg(event.getPhone() + "用户注册成功");
        loginLogMapper.save(loginLog);
    }

    /**
     * 监听登录信息
     */
    @EventListener
    public void OnListenLogin(@NotNull UserLoginEvent event) {
        log.info("用户登录成功，手机号:{}",event.getPhone());
        //记录日志
        LoginLog loginLog = new LoginLog();
        loginLog.setStatus(BaseStatus.ENABLE);
        loginLog.setIpaddr(UUID.randomUUID().toString());
        loginLog.setMsg(event.getPhone() + "用户登录成功");
        loginLogMapper.save(loginLog);
    }

    /**
     * 监听找回密码信息
     */
    @EventListener
    public void OnListenFindPassword(@NotNull UserFindPassowrdEvent event) {
        log.info("用户找回密码成功，手机号:{}",event.getPhone());
        //记录日志
        LoginLog loginLog = new LoginLog();
        loginLog.setStatus(BaseStatus.ENABLE);
        loginLog.setIpaddr(UUID.randomUUID().toString());
        loginLog.setMsg(event.getPhone() + "用户找回密码成功");
        loginLogMapper.save(loginLog);
    }

}
